home *** CD-ROM | disk | FTP | other *** search
/ PC Basics 53 / PC Basics Issue 53.iso / Software / Internet / Invboard.exe / PC Basics 53 / Invboard / upload / sources / lib / add_poll.php next >
Encoding:
PHP Script  |  2002-06-12  |  5.0 KB  |  174 lines

  1. <?php
  2.  
  3. /*
  4. +--------------------------------------------------------------------------
  5. |   IBFORUMS v1
  6. |   ========================================
  7. |   by Matthew Mecham and David Baxter
  8. |   (c) 2001,2002 IBForums
  9. |   http://www.ibforums.com
  10. |   ========================================
  11. |   Web: http://www.ibforums.com
  12. |   Email: phpboards@ibforums.com
  13. |   Licence Info: phpib-licence@ibforums.com
  14. +---------------------------------------------------------------------------
  15. |
  16. |   > Add POLL module
  17. |   > Module written by Matt Mecham
  18. |
  19. +--------------------------------------------------------------------------
  20. |
  21. |   QUOTE OF THE MODULE: (Taken from BtVS)
  22. |   --------------------
  23. |    Drusilla: I'm naming all the stars...
  24. |   Spike: You can't see the stars love, That's the ceiling. Also, it's day.
  25. |
  26. +-------------------------------------------------------------------------- 
  27. */
  28.  
  29.  
  30. $idx = new Poll;
  31.  
  32. class Poll {
  33.  
  34.  
  35.     var $topic = array();
  36.     var $poll  = array();
  37.     var $upload = array();
  38.     var $poll_count = 0;
  39.     var $poll_choices = "";
  40.  
  41.     function Poll() {
  42.     
  43.         global $ibforums, $std, $DB, $print;
  44.         
  45.         $ibforums->lang      = $std->load_words($ibforums->lang, 'lang_post', $ibforums->lang_id);
  46.         
  47.         // Lets do some tests to make sure that we are allowed to start a new topic
  48.         
  49.         if (! $ibforums->member['g_vote_polls'])
  50.         {
  51.             $std->Error( array( LEVEL => 1, MSG => 'no_reply_polls') );
  52.         }
  53.         
  54.         // Did we choose a choice?
  55.         
  56.         if (!$ibforums->input['nullvote'])
  57.         {
  58.             if (! isset($ibforums->input['poll_vote']) )
  59.             {
  60.                 $std->Error( array( LEVEL => 1, MSG => 'no_vote') );
  61.             }
  62.         }
  63.  
  64.         // Make sure we have a valid poll id
  65.         
  66.            $ibforums->input[t] = $std->is_number($ibforums->input[t]);
  67.         if (! $ibforums->input[t] ) {
  68.             $std->Error( array( LEVEL => 1, MSG => 'missing_files') );
  69.         }
  70.    
  71.            // Load the topic and poll
  72.            
  73.            $DB->query("SELECT t.*, p.pid as poll_id,p.choices,p.starter_id,p.votes from ibf_polls p, ibf_topics t WHERE t.tid='".$ibforums->input['t']."' and p.tid=t.tid");
  74.            
  75.            $this->topic = $DB->fetch_row();
  76.            
  77.            if (! $this->topic['tid'] )
  78.            {
  79.                $std->Error( array( LEVEL => 1, MSG => 'poll_none_found') );
  80.            }
  81.  
  82.            if ($this->topic['state'] != 'open')
  83.            {
  84.                $std->Error( array( LEVEL => 1, MSG => 'locked_topic') );
  85.            }
  86.         // Have we voted before?
  87.         
  88.         $DB->query("SELECT member_id from ibf_voters WHERE tid='".$this->topic['tid']."' and member_id='".$ibforums->member['id']."'");
  89.         
  90.         if ( $DB->get_num_rows() )
  91.         {
  92.             $std->Error( array( LEVEL => 1, MSG => 'poll_you_voted') );
  93.         }
  94.         
  95.         // If we're here, lets add the vote
  96.         
  97.         
  98.         $db_string = $std->compile_db_string(
  99.                                               array (
  100.                                                           'member_id'  => $ibforums->member['id'],
  101.                                                           'ip_address' => $ibforums->input['IP_ADDRESS'],
  102.                                                           'tid'        => $this->topic['tid'],
  103.                                                           'forum_id'   => $this->topic['forum_id'],
  104.                                                           'vote_date'  => time(),
  105.                                                       )
  106.                                               );
  107.                                               
  108.         $DB->query("INSERT INTO ibf_voters (" .$db_string['FIELD_NAMES']. ") VALUES (". $db_string['FIELD_VALUES'] .")");
  109.  
  110.  
  111.         // If this isn't a null vote...
  112.         
  113.         if (!$ibforums->input['nullvote'])
  114.         {
  115.             $poll_answers = unserialize(stripslashes($this->topic['choices']));
  116.             reset($poll_answers);
  117.             $new_poll_array = array();
  118.             foreach ($poll_answers as $entry)
  119.             {
  120.                 $id     = $entry[0];
  121.                 $choice = $entry[1];
  122.                 $votes  = $entry[2];
  123.                 
  124.                 if ($id == $ibforums->input['poll_vote'])
  125.                 {
  126.                     $votes++;
  127.                 }
  128.                 
  129.                 $new_poll_array[] = array( $id, $choice, $votes);
  130.             }
  131.             
  132.             $this->topic['choices'] = addslashes(serialize($new_poll_array));
  133.             
  134.             $DB->query("UPDATE ibf_polls SET ".
  135.                          "votes=votes+1, ".
  136.                          "choices='"  . $this->topic['choices'] . "' ".
  137.                          "WHERE pid='" . $this->topic['poll_id']    . "'");
  138.                          
  139.             if ($ibforums->vars['allow_poll_bump'])
  140.             {
  141.             
  142.                 $this->topic['last_vote'] = time();
  143.                 $this->topic['last_post'] = time();
  144.  
  145.                 $DB->query("UPDATE ibf_topics SET ".
  146.                              "last_vote='" . $this->topic['last_vote'] . "', ".
  147.                              "last_post='" . $this->topic['last_post'] . "' ".
  148.                              "WHERE tid='" . $this->topic['tid']       . "'");
  149.                              
  150.             }
  151.             else
  152.             {
  153.                 $this->topic['last_vote'] = time();
  154.                 
  155.                 $DB->query("UPDATE ibf_topics SET ".
  156.                              "last_vote='" . $this->topic['last_vote'] . "', ".
  157.                              "last_post='" . $this->topic['last_post'] . "' ".
  158.                              "WHERE tid='" . $this->topic['tid']       . "'");
  159.                              
  160.             }
  161.             
  162.             
  163.         }
  164.  
  165.         $lang = $ibforums->input['nullvote'] ? $ibforums->lang['poll_viewing_results'] : $ibforums->lang['poll_vote_added'];
  166.         
  167.         $print->redirect_screen( $lang , "act=ST&f={$this->topic['forum_id']}&t={$this->topic['tid']}" );
  168.  
  169.  
  170.     }
  171.  
  172. }
  173.  
  174. ?>